package com.quan.api.oms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.commons.core.bean.R;
import com.quan.commons.core.biz.support.MyBaseController;
import com.quan.commons.core.configuration.WxPayConfiguration;
import com.quan.commons.core.exception.BizException;
import com.quan.commons.core.properties.WxPayProperties;
import com.quan.commons.core.utils.PageUtils;
import com.quan.commons.log.annotation.SysLog;
import com.quan.mall.commons.vo.OmsOrderVo;
import com.quan.mall.commons.vo.OrderConfirmVo;
import com.quan.mall.entity.OmsOrder;
import com.quan.mall.service.OmsOrderService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;


/**
 * 订单
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2021-03-09 17:22:37
 */
@Slf4j
@Api(tags = "订单")
@RestController
@RequestMapping("/api/mall/omsorder")
public class ApiOmsOrderController extends MyBaseController {

    @Autowired
    private OmsOrderService service;

    @Autowired
    private RedissonClient redissonClient;


    /**
     * 订单确认页返回需要用的数据
     * @param vo
     * @return
     */
    @PostMapping(value = "/confirmOrder")
    public R confirmOrder(@Valid @RequestBody OrderConfirmVo vo) {
        OrderConfirmVo confirmVo =  this.service.confirmOrder(vo);

        if(null != confirmVo) {
            return R.ok().put("data", confirmVo);
        } else {
            return R.failure("确认订单数据失败");
        }
    }

    /**
     * 会员下单
     * @param vo
     * @return
     */
    @PostMapping(value = "/submitOrder")
    public R submitOrder(@Valid @RequestBody OmsOrderVo vo) {
        //1、获取一把锁，锁的名称随便起，只要锁的名字相同就是同一把锁
        final RLock lock = this.redissonClient.getLock("CreateOrder");

        //2、加锁
        lock.lock();
        try {
            final WxPayProperties.Config config = WxPayConfiguration.getAppIdConfig(vo.getAppId());
            vo.setMchId(config.getMchId());
            return this.service.submitOrder(vo);
        } catch (Exception e) {
            log.error("订单提交失败：{}", e);
            return R.failure(e.getMessage());
        } finally {
            //释放锁
            lock.unlock();
        }
    }

    /**
     * 会员确认收货
     * @param vo
     * @return
     */
    @GetMapping("/memberConfirmReceivePack")
    public R memberConfirmReceivePack(OmsOrderVo vo) {
        this.service.memberConfirmReceivePack(vo);
        return R.ok();
    }

    /**
     * 订单分页
     * @param vo
     * @return
     */
    @GetMapping(value = "page")
    public R queryPage(OmsOrderVo vo) {
        PageUtils page = this.service.queryPage(new PageUtils(request), vo);
        return R.ok().put("data", page);
    }

    /**
     * 查询订单详情
     * @param {id}
     * @return
     */
    @GetMapping("/getOrderInfo/{id}")
    public R getOrderInfo(@PathVariable("id") Long id) {
        OmsOrderVo omsOrder = this.service.getOrderInfo(id);
        return R.ok().put("data", omsOrder);
    }


}
